package com.kadron.datamodel;

import java.util.List;

import javax.persistence.EntityManager;

import com.kadron.dao.KEmbedDAO;
import com.kadron.entity.Embed;

public class KEmbedModelDataProvider implements KDataProvider {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4707252300895840420L;
	
	private KModelCreator creator;
	private KEmbedDAO embedDAO;
	
	public KEmbedModelDataProvider(KModelCreator creator, KEmbedDAO embedDAO) {
		super();
		this.creator = creator;
		this.embedDAO = embedDAO;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Long> getItemsByRange(Integer integer, int numberOfRows,
			Object object, boolean b) {
		EntityManager em = embedDAO.getEntityManager();
		return em.createQuery("SELECT a.id FROM Embed a ORDER BY a.id DESC")
			.setFirstResult(integer)
			.setMaxResults(numberOfRows)
			.getResultList();
	}

	@Override
	public KModel getItemById(Long id, KModelCreator creator) {
		return embedDAO.find(id, Embed.class);
	}

	@Override
	public int getRowCount() {
		EntityManager em = embedDAO.getEntityManager();
		Long count = (Long) em.createQuery("SELECT COUNT(a.id) FROM Embed a")
			.getSingleResult();
		return (count == null) ? 0 : count.intValue() ;
	}

	@Override
	public boolean hasItemWithId(Long id) {
		return embedDAO.exists(id, Embed.class);
	}

	@Override
	public KModelCreator getCreator() {
		return creator;
	}

}
